package com.agw.crm.settings.service.impl;

import com.agw.crm.exception.LoginException;
import com.agw.crm.settings.dao.UserDao;
import com.agw.crm.settings.domain.User;
import com.agw.crm.settings.service.UserService;
import com.agw.crm.utils.DateTimeUtil;
import com.agw.crm.utils.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    @Override
    public User login(String loginAct, String loginPwd, String ip) throws LoginException {

        //使用map集合储存参数
        Map<String,String> map = new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        //调用方法进行验证
        User user = userDao.login(map);

        //如果user为空,账户密码错误
        if (user == null){
            throw new LoginException("账号密码错误");
        }
        //获取到失效时间
        String expireTime = user.getExpireTime();
        //调用方法获取到现在的时间
        String nowTime = DateTimeUtil.getSysTime();
        if (expireTime.compareTo(nowTime) < 0){
            throw new LoginException("账号已失效，请联系安帅哥帮您改正");
        }

        //验证锁定状态
        String lockState = user.getLockState();
        if ("0".equals(lockState)){
            throw new LoginException("账号已锁定，请联系安帅哥帮您改正");
        }

        //验证ip地址
        String allowIps = user.getAllowIps();
        if (!allowIps.contains(ip)){
            throw new LoginException("ip地址受限，请联系安帅哥解决");
        }

        return user;
    }


    @Override
    public List<User> getUserList() {
        List<User> list = userDao.getUserList();
        return list;
    }
}
